package com.baosight.sczx.zm.cc.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblem;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblemHandleProcess_x;

/**
 * 生产问题整改反馈模块
 * 生产问题责任 部门操作
 * 已整改生产问题
 * <p>Title: ServiceZMCC21.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-8-6 上午11:13:34  严洪浩  Create
 *</pre>
 */
public class ServiceZMCC21 extends ServiceEPBase {

	/**
	 * 初始进入
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCL70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-5上午07:50:22 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);

		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCL70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-5上午07:51:16 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
//		String miningCode = UserSession.getInSessionProperty("miningCode").toString();
//		StaticTools.setQueryParameters(inInfo, "fdMiningId", miningCode, true);
//		
//		if (inInfo.getBlock(EiConstant.queryBlock) == null) {
//			StaticTools.setQueryParameters(inInfo, "fdEndDate", StaticTools.getDate(), true);// 截止日期
//		}
//		StaticTools.setQueryParameters(inInfo, "fdBusinessDepartmentId", UserSession.getInSessionProperty("orgCode"), true);// 隐患部门
//		StaticTools.setQueryParameters(inInfo, "isModifyed", "yes", true);// 所有已整改隐患
//		EiInfo outInfo = super.query(inInfo, "ZMCCProduceproblem.query", new ZMCCProduceproblem());
		
		EiInfo outInfo = new EiInfo();
		EiBlock resultBlock = new EiBlock("result");
		ZMCCProduceproblemHandleProcess_x  _dom= new ZMCCProduceproblemHandleProcess_x();
		resultBlock.addBlockMeta(_dom.eiMetadata);
		Map<String, String> _args = new HashMap<String, String>();
		if (inInfo.getBlock(EiConstant.queryBlock) == null) {
			_args.put("fdEndDate", StaticTools.getDate());// 截止日期
		}
		_args.put("isModifyed", "yes"); //所有已整改隐患
		_args.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString());
		_args.put("fdBusinessDepartmentId", UserSession.getInSessionProperty("orgCode").toString());
		
		List<ZMCCProduceproblem> _result = dao.query("ZMCCProduceproblem.query", _args);
		for (int i = 0; i < _result.size(); i++) {
			ZMCCProduceproblem record = (ZMCCProduceproblem) _result.get(i);
			Map<String, String> process_args = new HashMap<String, String>();
			process_args.put("fdRecordId", record.getFdKeyId());
			List<ZMCCProduceproblemHandleProcess_x> process_result = dao.query("ZMCCProduceproblemHandleProcess_x.query", process_args);
			resultBlock.addRows(process_result);
		}
		outInfo.addBlock(resultBlock);
		
		outInfo.addBlock(getMines());
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemType", false));// 问题类型
		outInfo.addBlock(StaticBlocks.getProblemStatus());// 问题状态
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemLevel", false));// 问题级别
		outInfo.addBlock(StaticBlocks.getIsCheck());// 是否核查
//		outInfo.addBlock(StaticBlocks.getProblemOrigin());// 问题来源
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemOrigin"));//问题来源 
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		return outInfo;
	}	
	
	public EiBlock getMines() {
		EiBlock block = new EiBlock("cmes.ch.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCCProduceproblem.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}
}
